package com.tianda.his.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.system.dto.MenuDto;
import com.tianda.his.modules.system.entity.SysMenu;
import com.tianda.his.modules.system.service.ISysMenuService;
import com.tianda.his.modules.system.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * 菜单（权限）管理
 */
@RestController
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    //角色菜单中间表service层
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 查询所有的菜单（权限）
     * @return
     */
    @GetMapping("/system/menu/selectMenuTree")
    public AjaxResult selectMenuTree() {
        return AjaxResult.success(sysMenuService.list());
    }

    /**
     * 根据角色id查询所有的菜单(权限)id
     * @param roleId
     * @return
     */
    @GetMapping("/system/menu/getMenuIdsByRoleId/{roleId}")
    public AjaxResult getMenuIdsByRoleId(@PathVariable("roleId") Long roleId) {
        List<Long> roleIds = sysMenuService.getMenuIdsByRoleId(roleId);
        return AjaxResult.success(roleIds);
    }

    /**
     * 为角色分配菜单
     * @param roleId 角色id
     * @param menuIds 菜单id集合
     * @return
     */
    @PostMapping("/system/role/saveRoleMenu/{roleId}/{menuIds}")
    public AjaxResult saveRoleMenu(@PathVariable Long roleId, @PathVariable List<Long>  menuIds) {
        sysRoleMenuService.saveRoleMenu(roleId,menuIds);
        return AjaxResult.success();
    }

    /**
     * 查询所有的菜单（权限）
     * @param menuDto
     * @return
     */
    @GetMapping("/system/menu/listAllMenus")
    public AjaxResult listAllMenus(MenuDto menuDto) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        //TODO 组织查询条件
        return AjaxResult.success(sysMenuService.list(queryWrapper));
    }

}
